package com.mt.takeway.controller;


import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.mt.takeway.po.Customer;
import com.mt.takeway.po.Sms;
import com.mt.takeway.service.CustomerService;

import com.mt.takeway.vo.CustomerVo;
import lombok.extern.slf4j.Slf4j;

import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/***
 *@title CustomerController
 *@author chl
 *@create 2022/5/26 21:39
 */
@RestController
@Slf4j
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    @CrossOrigin
    @PostMapping("/sendCode")
    public String sms(@RequestBody Sms sms){
        int appid=1400684236;
        String appKey="a79ccc396002017e2defa486b7200b3f";
        int templateId=1417759;
        String smsSign="CCriverSouth";

        //短信六位验证码
        Long codeL = System.nanoTime();
        String codeStr = Long.toString(codeL);
        String verifyCode = codeStr.substring(codeStr.length() - 6);
        log.info("短信验证码"+verifyCode);

        sms.setCode(verifyCode);
        sms.setMin(10);

        log.info(sms.toString());

        try {
            String[] params = {sms.getCode(),Integer.toString(sms.getMin())};
            SmsSingleSender ssender = new SmsSingleSender(appid, appKey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", sms.getPhoneNumber(), templateId,
                    params, smsSign, "", "");
            log.info(result.toString());

            Customer customer = new Customer();
            customer.setTel(sms.getPhoneNumber());
            customer.setPassword(sms.getCode());
            boolean b = customerService.registerCustomer(customer);
            if (b) return "验证码获取成功";
        }catch (JSONException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        } catch (HTTPException e) {
            e.printStackTrace();
        }

        return "验证码获取失败";
    }

    @CrossOrigin
    @PostMapping("/register")
    public CustomerVo registerCustomer(@RequestBody Customer customer){
//        log.info(customer.getTel());
            Customer cust = customerService.findCustomerByTel(customer.getTel(), customer.getPassword());
            log.info(cust.toString());

            CustomerVo customerVo = null;
            Customer customer1 = customerService.findOneCustomer(customer.getTel());
            log.info(customer1.toString());

            if (customer1.getPassword().equals(cust.getPassword()))
                return new CustomerVo(cust,"注册成功");



        return new CustomerVo(null,"注册失败");

    }

    @CrossOrigin
    @PostMapping("/dealLogin")
    public CustomerVo loginCustomer(String tel,String password){
        log.info(tel+"abc"+password);

        CustomerVo customerVo = null;

        Customer customer = customerService.findCustomerByTel(tel,password);

        if (customer != null) return new CustomerVo(customer,"登录成功");

        return new CustomerVo(null,"登录失败");
    }


}
